package com.example.testapplication;

import android.support.annotation.NonNull;
import android.support.v4.util.ArrayMap;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import java.util.List;
import java.util.Map;

/**
 * @author wwq on 2019/7/23  17:08.
 */
public class ProjectPhaseRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    View view;

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView category;
        private CheckBox cbProjectPhase;

        ViewHolder(@NonNull View itemView) {
            super(itemView);
            category = itemView.findViewById(R.id.category);
            cbProjectPhase = itemView.findViewById(R.id.cbProjectPhase);
        }
    }

    private List<Category> categoryList;

    ProjectPhaseRecyclerViewAdapter(List<Category> list) {
        this.categoryList = list;
        initMap();
    }

    private Map<Integer, Boolean> map = new ArrayMap<>();

    private void initMap() {
        for (int i = 0; i < categoryList.size(); i++) {
            map.put(i, false);
        }
    }

    Map<Integer, Boolean> getMap() {
        return map;
    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_project_phase, viewGroup, false);
        return new ViewHolder(view);

    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, final int i) {
        Category category = categoryList.get(i);
        ((ViewHolder) viewHolder).category.setText(category.getCategory());

        ((ViewHolder) viewHolder).cbProjectPhase.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    map.put(i, true);
                } else {
                    map.put(i, false);
                }
            }
        });
        if (map.get(i)) {
            ((ViewHolder) viewHolder).cbProjectPhase.setChecked(true);
        } else {
            ((ViewHolder) viewHolder).cbProjectPhase.setChecked(false);
        }
    }


    @Override
    public int getItemCount() {
        return categoryList.size();
    }


}
